package com.salatigacode.customerdatabase.dao;

import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;

import com.salatigacode.customerdatabase.entity.Customer;

public class CustomerDAOSpring implements CustomerDAOInterface {
	private DataSource ds;
	private JdbcTemplate jdbcTemplateObject;

	public CustomerDAOSpring(DataSource ds) {
		this.ds = ds;
		this.jdbcTemplateObject = new JdbcTemplate(this.ds);
	}

	@Override
	public List<Customer> getAll() {
		String sql = "SELECT id,firstname,lastname,phone,email FROM tbl_customer";
		List<Customer> customers = jdbcTemplateObject.query(sql,
				new CustomerMapper());
		return customers;
	}

	@Override
	public void delete(int id) {
		String sql = "DELETE FROM tbl_customer WHERE id=?";
		this.jdbcTemplateObject.update(sql, id);
	}

	@Override
	public void insert(Customer customer) {
		String sql = "INSERT INTO tbl_customer(firstname,lastname,phone,email) "
				+ "VALUES(?,?,?,?)";
		this.jdbcTemplateObject.update(sql, customer.getFirstName(),
				customer.getLastName(), customer.getPhone(),
				customer.getEmail());
	}
}
